home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / PhysicalDevice.py < prev    next >
Encoding:
Python Source  |  2009-05-05  |  5.4 KB  |  161 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Copyright (C) 2008, 2009 Tim Waugh <twaugh@redhat.com>
  4. ## Copyright (C) 2008, 2009 Red Hat, Inc.
  5.  
  6. ## This program is free software; you can redistribute it and/or modify
  7. ## it under the terms of the GNU General Public License as published by
  8. ## the Free Software Foundation; either version 2 of the License, or
  9. ## (at your option) any later version.
  10.  
  11. ## This program is distributed in the hope that it will be useful,
  12. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ## GNU General Public License for more details.
  15.  
  16. ## You should have received a copy of the GNU General Public License
  17. ## along with this program; if not, write to the Free Software
  18. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. from gettext import gettext as _
  21. import cupshelpers
  22.  
  23. class PhysicalDevice:
  24.     def __init__(self, device):
  25.         self.devices = None
  26.         self.add_device (device)
  27.         self._user_data = {}
  28.  
  29.     def _canonical_id (self, device):
  30.         mfg = device.id_dict.get ('MFG', '')
  31.         mdl = device.id_dict.get ('MDL', '')
  32.  
  33.         if mfg == '' or mdl.lower ().startswith (mfg.lower ()):
  34.             make_and_model = mdl
  35.         else:
  36.             make_and_model = "%s %s" % (mfg, mdl)
  37.  
  38.         return cupshelpers.ppds.ppdMakeModelSplit (make_and_model)
  39.  
  40.     def add_device (self, device):
  41.         (mfg, mdl) = self._canonical_id (device)
  42.         if self.devices == None:
  43.             self.mfg = mfg
  44.             self.mdl = mdl
  45.             self.mfg_lower = mfg.lower ()
  46.             self.mdl_lower = mdl.lower ()
  47.             self.sn = device.id_dict.get ('SN', '')
  48.             self.devices = []
  49.         else:
  50.             def nicest (a, b):
  51.                 def count_lower (s):
  52.                     l = s.lower ()
  53.                     n = 0
  54.                     for i in xrange (len (s)):
  55.                         if l[i] != s[i]:
  56.                             n += 1
  57.                     return n
  58.                 if count_lower (b) < count_lower (a):
  59.                     return b
  60.                 return a
  61.  
  62.             self.mfg = nicest (self.mfg, mfg)
  63.             self.mdl = nicest (self.mdl, mdl)
  64.  
  65.             sn = device.id_dict.get ('SN', '')
  66.             if sn != '' and self.sn != '' and sn != self.sn:
  67.                 raise RuntimeError
  68.  
  69.         self.devices.append (device)
  70.         self.devices.sort ()
  71.  
  72.     def get_devices (self):
  73.         return self.devices
  74.  
  75.     def get_info (self):
  76.         # If the manufacturer/model is not known, or useless (in the
  77.         # case of the hpfax backend), show the device-info field
  78.         # instead.
  79.         if self.mfg == '' or (self.mfg == "HP" and self.mdl == "Fax"):
  80.             return self.devices[0].info
  81.  
  82.         info = "%s %s" % (self.mfg, self.mdl)
  83.         if len (self.sn) > 0:
  84.             info += " (%s)" % self.sn
  85.         return info
  86.  
  87.     # User data
  88.     def set_data (self, key, value):
  89.         self._user_data[key] = value
  90.  
  91.     def get_data (self, key):
  92.         return self._user_data.get (key)
  93.  
  94.     def __str__ (self):
  95.         return "(description: %s)" % self.__repr__ ()
  96.  
  97.     def __repr__ (self):
  98.         return "<PhysicalDevice.PhysicalDevice (%s,%s,%s)>" % (self.mfg,
  99.                                                                self.mdl,
  100.                                                                self.sn)
  101.  
  102.     def __cmp__(self, other):
  103.         if other == None or type (other) != type (self):
  104.             return 1
  105.  
  106.         if (other.mfg == '' and other.mdl == '') or \
  107.            (self.mfg == '' and self.mdl == ''):
  108.             # One or other is just a backend, not a real physical device.
  109.             if other.mfg == '' and other.mdl == '' and \
  110.                self.mfg == '' and self.mdl == '':
  111.                 return cmp (self.devices[0], other.devices[0])
  112.  
  113.             if other.mfg == '' and other.mdl == '':
  114.                 return -1
  115.             return 1
  116.  
  117.         if self.mfg == '' or self.mdl.lower ().startswith (self.mfg.lower ()):
  118.             our_make_and_model = self.mdl
  119.         else:
  120.             our_make_and_model = "%s %s" % (self.mfg, self.mdl)
  121.         (our_mfg, our_mdl) = \
  122.             cupshelpers.ppds.ppdMakeModelSplit (our_make_and_model)
  123.  
  124.         if other.mfg == '' or \
  125.                 other.mdl.lower ().startswith (other.mfg.lower ()):
  126.             other_make_and_model = other.mdl
  127.         else:
  128.             other_make_and_model = "%s %s" % (other.mfg, other.mdl)
  129.         (other_mfg, other_mdl) = \
  130.             cupshelpers.ppds.ppdMakeModelSplit (other_make_and_model)
  131.  
  132.         mfgcmp = cmp (our_mfg.lower (), other_mfg.lower ())
  133.         if mfgcmp != 0:
  134.             return mfgcmp
  135.         mdlcmp = cmp (our_mdl.lower (), other_mdl.lower ())
  136.         if mdlcmp != 0:
  137.             return mdlcmp
  138.         if self.sn == '' or other.sn == '': return 0;
  139.         return cmp (self.sn, other.sn)
  140.  
  141. if __name__ == '__main__':
  142.     import authconn
  143.     c = authconn.Connection ()
  144.     devices = cupshelpers.getDevices (c)
  145.  
  146.     physicaldevices = []
  147.     for device in devices.values ():
  148.         physicaldevice = PhysicalDevice (device)
  149.         try:
  150.             i = physicaldevices.index (physicaldevice)
  151.             physicaldevices[i].add_device (device)
  152.         except ValueError:
  153.             physicaldevices.append (physicaldevice)
  154.  
  155.     physicaldevices.sort ()
  156.     for physicaldevice in physicaldevices:
  157.         print physicaldevice.get_info ()
  158.         devices = physicaldevice.get_devices ()
  159.         for device in devices:
  160.             print " ", device
  161.